<?php
	/**
	* Log Manager  - Get Log in jSon
	*/
	if (!defined('EXPONENT')) exit('');
	 if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate"),$loc)){
		$type_error = 0;
		$sql_where = '1 ';
		$page=1;
		$log = null;

		if(isset($_REQUEST['from_date']))
		{   
			$from_date = strtotime($_REQUEST['from_date']);
			$sql_where.=" AND (error_date >= {$from_date})";
		}
		if(isset($_REQUEST['to_date']))
		{   
			$to_date = strtotime($_REQUEST['to_date']);
			$sql_where.=" AND (error_date <= {$to_date})";
		}
		
		if(isset($_REQUEST['type_error']))
		{   
			$type_error = intval($_REQUEST['type_error']);
			if ($type_error > -1)
			$sql_where .= " AND (error_type = {$type_error})";
		}
		
		// sort
		$sort="";
		if (isset($_REQUEST['sortName']))
		{
			$sort=$_REQUEST['sortName'];
			// for some field has been faked, we should turn them into the right field name
			if ($sort == "error_date_string")
				$sort = "error_date";
				
			$sortOrder="ASC";
			if (isset($_REQUEST['sortOrder']) && ($_REQUEST['sortOrder'] != "asc"))
				$sortOrder = "DESC";
			$sort = " ORDER BY {$sort} {$sortOrder}";
		}
		else
			$sort = "ORDER BY error_date DESC";
		
		if (isset($_REQUEST['page']))
			$page=intval($_REQUEST['page']);
		$pageSize=10;
		
		if (isset($_REQUEST['pageSize']))
			$pageSize=intval($_REQUEST['pageSize']);
		$start=($page-1)*$pageSize;
		
		$data=$db->selectObjects('syslog',"{$sql_where} {$sort} LIMIT {$start},{$pageSize}");
		
		foreach ($data as $k=>$v)
		{
			$v->error_date_string = date("F j, Y g:i:s a", $v->error_date);
		}
		
		$ret_object = null;
		
		$ret_object->total = $db->countObjects('syslog',"{$sql_where}");
		$ret_object->rows = $data; 
		echo json_encode($ret_object);
	}
	die();
?>